<template>
  <view>
    <view class="search-box">
      <!-- 搜索 -->
      <my-search></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) of swiperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id' + item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 分类导航 -->
    <view class="nav-list">
      <navigator v-for="(nav, i) of navList" :key="i" class="nav-item" @click="navClickHandler(nav)">
        <image :src="nav.image_src" class="nav-image"></image>
      </navigator>
    </view>
    <!-- 楼层导航 -->
    <view class="floorList" v-for="(floor, i) in floorList" :key="i">
      <!-- 楼层标题导航 -->
      <view class="floorTitle"><image :src="floor.floor_title.image_src" class="floorImage"></image></view>
      <!-- 楼层图片 -->
      <view class="floorItem">
        <!-- 左侧小盒子 -->
        <navigator class="floorLeftBOX" :url="floor.product_list[0].url">
          <image
            :src="floor.product_list[0].image_src"
            class="floorleftImage"
            :style="{ width: floor.product_list[0].image_width + 'rpx' }"
            mode="widthFix"
          ></image>
        </navigator>
        <!-- 右侧小盒子 -->
        <view class="floorRightBox">
          <navigator
            :url="item.url"
            class="floorRightItem"
            v-for="(item, i2) in floor.product_list"
            :key="i2"
            v-if="i2 !== 0"
          >
            <image
              :src="item.image_src"
              class="floorRightImage"
              mode="widthFix"
              :style="{ width: item.image_width + 'rpx' }"
            ></image>
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import badgeMix from '@/mixins/tabbar_badge.js';
export default {
  mixins: [badgeMix],
  data() {
    return {
      swiperList: [],
      navList: [],
      floorList: []
    };
  },
  // onLoad() {
  //   this.getSwiperList();
  //   this.getNavList();
  //   this.getFloorList();
  // },
  methods: {
    async getSwiperList() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata');
      if (res.meta.status !== 200) return uni.$showMsg();
      this.swiperList = res.message;
    },
    async getNavList() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/catitems');
      if (res.meta.status !== 200) return uni.$showMsg('nav数据获取失败');
      this.navList = res.message;
    },
    navClickHandler(nav) {
      if (nav.name === '分类') {
        // 移动至"分类"
        uni.switchTab({
          url: '/pages/cate/cate'
        });
      }
    },
    async getFloorList() {
      const { data: res } = await uni.$http.get('/api/public/v1/home/floordata');
      if (res.meta.status !== 200) return uni.$showMsg();
      res.message.forEach(floor => {
        floor.product_list.forEach(p => {
          p.url = '/subpkg/goods_list/goods_list?' + p.navigator_url.split('?')[1];
        });
      });
      this.floorList = res.message;
    }
  },
  onShow() {
    this.getSwiperList();
    this.getNavList();
    this.getFloorList();
  }
};
</script>

<style lang="scss">
.search-box {
  position: sticky;
  top: 0;
  z-index: 9999;
}
swiper {
  height: 330rpx;
}
.swiper-item {
  image {
    width: 100%;
  }
}
.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;
  .nav-image {
    width: 128rpx;
    height: 140rpx;
  }
}
.floorTitle {
  height: 60rpx;
  width: 100%;
  .floorImage {
    width: 100%;
    height: 100%;
  }
}
.floorItem {
  display: flex;
  padding-left: 14rpx;
}
.floorRightBox {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}
</style>
